package com.elite.groovy.dev;

import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.masterreplica.MasterReplica;
import io.lettuce.core.masterreplica.StatefulRedisMasterReplicaConnection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

public class RedisSentinelDev {
    private static final Log log = LogFactory.getLog(RedisSentinelDev.class);

    public static void main(String[] args) throws Exception {
        // JedisSentinelPool();
        // LettuceSentinel();

        dateToStamp("2024-12-15 00:50:40");

        log.debug("ℹ JedisSentinel:" + dateToStamp("2024-12-15 00:50:40"));
    }

    public static String dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (Exception e) {
            log.error("dateToStamp Exception:" + e.getMessage());
        }

        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    public static void JedisSentinelPool() {
        HashSet<String> sentinels = new HashSet<>();
        sentinels.add("10.252.7.81:26379");
        sentinels.add("10.252.7.82:26379");
        sentinels.add("10.252.7.83:26379");
        JedisSentinelPool pool = new JedisSentinelPool("mymaster2", sentinels, "Ja>Cf35/4816");
        Jedis jedis = pool.getResource();
        jedis.set("hello", "world");
        String redis_Res = jedis.get("hello");

        log.debug("ℹ JedisSentinel:" + sentinels + " ⌛查询结果:" + redis_Res);

    }

    public static void LettuceSentinel() {
        //集群节点
        List<RedisURI> uris = new ArrayList();
        uris.add(RedisURI.builder().withSentinel("10.252.7.81", 26379).withSentinelMasterId("mymaster2").withPassword("Ja>Cf35/4816").build());
        uris.add(RedisURI.builder().withSentinel("10.252.7.82", 26379).withSentinelMasterId("mymaster2").withPassword("Ja>Cf35/4816").build());
        uris.add(RedisURI.builder().withSentinel("10.252.7.83", 26379).withSentinelMasterId("mymaster2").withPassword("Ja>Cf35/4816").build());

        RedisClient client = RedisClient.create();
        StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica.connect(client, StringCodec.UTF8, uris);
        //从节点读取数据
        connection.setReadFrom(ReadFrom.REPLICA);

        RedisCommands<String, String> commands = connection.sync();
        commands.set("hello", "赵云");
        System.out.println(commands.get("hello"));

        connection.close();
        client.shutdown();
    }
}